package cn.iocoder.yudao.module.system.convert.mail;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.*;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 邮件账户转换
 *
 * @author huangweidong
 * @date 2023/05/10
 */
@Mapper
public interface MailAccountConvert {

    MailAccountConvert INSTANCE = Mappers.getMapper(MailAccountConvert.class);

    /**
     * 转换
     *
     * @param bean bean
     * @return {@code MailAccountDO}
     */
    MailAccountDO convert(MailAccountCreateReqVO bean);

    /**
     * 转换
     *
     * @param bean bean
     * @return {@code MailAccountDO}
     */
    MailAccountDO convert(MailAccountUpdateReqVO bean);

    /**
     * 转换
     *
     * @param bean bean
     * @return {@code MailAccountRespVO}
     */
    MailAccountRespVO convert(MailAccountDO bean);

    /**
     * 转换分页
     *
     * @param pageResult 分页结果
     * @return {@code PageResult<MailAccountBaseVO>}
     */
    PageResult<MailAccountBaseVO> convertPage(PageResult<MailAccountDO> pageResult);

    /**
     * 转换list02
     *
     * @param list 列表
     * @return {@code List<MailAccountSimpleRespVO>}
     */
    List<MailAccountSimpleRespVO> convertList02(List<MailAccountDO> list);

    /**
     * 转换
     *
     * @param account  账户
     * @param nickname 昵称
     * @return {@code MailAccount}
     */
    default MailAccount convert(MailAccountDO account, String nickname) {
        String from = StrUtil.isNotEmpty(nickname) ? nickname + " <" + account.getMail() + ">" : account.getMail();
        return new MailAccount().setFrom(from).setAuth(true)
                .setUser(account.getUsername()).setPass(account.getPassword())
                .setHost(account.getHost()).setPort(account.getPort()).setSslEnable(account.getSslEnable());
    }

}
